package com.example.mysqlview;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

public class EditActivity extends AppCompatActivity {
    private EditText et_id, et_name, et_price;
    private Button bt_tj;
    private String id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        Intent intent = getIntent();
        id = intent.getStringExtra("ID");
        String name = intent.getStringExtra("NAME");
        int price = intent.getIntExtra("PRICE", 0);

        et_id = findViewById(R.id.et_id);
        et_name = findViewById(R.id.et_name);
        et_price = findViewById(R.id.et_price);
        bt_tj = findViewById(R.id.bt_tj);

        et_id.setText(id);
        et_name.setText(name);
        et_price.setText(String.valueOf(price));

        bt_tj.setOnClickListener(view -> {
            MySQLiteOpenHelper helper = new MySQLiteOpenHelper(EditActivity.this,
                    "fru.db", null, 1);
            SQLiteDatabase writableDatabase = helper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", et_name.getText().toString());
            values.put("price", Integer.parseInt(et_price.getText().toString()));
            String where = "id='" + id+"'";
            writableDatabase.update("fruit", values, where, null);
            writableDatabase.close();
            finish();
        });
    }
}